package cn.dyp.com.automaticcontrolclient.fragment;


import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import cn.dyp.com.automaticcontrolclient.R;
import cn.dyp.com.automaticcontrolclient.ClientApp;
import cn.dyp.com.automaticcontrolclient.request.BaseRequest;
import cn.dyp.com.automaticcontrolclient.request.RequestThread;
import cn.dyp.com.automaticcontrolclient.request.SetConfigRequest;

/**
 * 系统设置Fragment类
 * 
 * @author asus
 *
 */
public class SettingFragment extends PreferenceFragment implements
		OnPreferenceChangeListener, OnPreferenceClickListener {
	private static final String AUTO = "auto";
	private static final String AUTOGROUP = "auto_group";

	private EditTextPreference mAutoControlETP;

	private SetConfigRequest mRequest;
	private RequestThread mThread;
	private ClientApp mApp;

	private RadioGroup mGroup;
	private String mCurrentStat;
	private int mAutoId = R.id.radio0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preference_setting);
		setApp();
		setRequest();
		initView();
		setListener();
	}

	/**
	 * 设置控制请求类
	 * 
	 */
	private void setRequest() {
		mRequest = new SetConfigRequest("");
	}

	/**
	 * 设置Application
	 * 
	 */
	private void setApp() {
		mApp = (ClientApp) getActivity().getApplication();
	}

	/**
	 * 设置监听器
	 * 
	 */
	private void setListener() {
		mAutoControlETP.setOnPreferenceChangeListener(this);
		mAutoControlETP.setOnPreferenceClickListener(this);
	}

	/**
	 * 初始化控件
	 * 
	 */
	private void initView() {
		// 获取到控件
		mAutoControlETP = (EditTextPreference) findPreference(AUTO);
		mAutoControlETP.getEditor().putInt(AUTOGROUP, mAutoId).commit();
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		switch (preference.getKey()) {
		case AUTO:
			autonControl();
			break;
		}
		return true;
	}

	/**
	 * 切换手动/自动控制
	 *
	 */
	private void autonControl() {
		// 保存数据
		mAutoControlETP.getEditor().putInt(AUTOGROUP, mAutoId).commit();
		if (mAutoId == R.id.radio0) {
			mCurrentStat = getString(R.string.manual);
			mRequest.setAutoControl(0);
		} else {
			mCurrentStat = getString(R.string.auto);
			mRequest.setAutoControl(1);
		}
		startRequest(mRequest);
		// 设置Summary的内容
		mAutoControlETP.setSummary(mCurrentStat);
	}

	/**
	 * 设置网络连接的线程
	 * 
	 * @param request
	 *            请求对象
	 */
	private void startRequest(BaseRequest request) {
		if (request != null) {
			mThread = new RequestThread(getActivity(), mApp.getHandler());
			mThread.setRequest(request);
			mThread.start();
		}
	}

	/**
	 * 设置控制状态的方式
	 */
	private void setAutoRadioButton() {
		mGroup = (RadioGroup) mAutoControlETP.getDialog().findViewById(
				R.id.radioGroup1);
		mAutoId = mAutoControlETP.getSharedPreferences().getInt(AUTOGROUP, 0);
		mGroup.check(mAutoId);
		((RadioButton) mGroup.getChildAt(0)).setText(R.string.manual);
		((RadioButton) mGroup.getChildAt(1)).setText(R.string.auto);
		mGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.radio0) {
					mAutoId = R.id.radio0;
				} else if (checkedId == R.id.radio1) {
					mAutoId = R.id.radio1;
				}
			}
		});
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		setAutoRadioButton();
		return true;
	}
}